<?php

/**
 * fournisseur actions.
 *
 * @package    appcemhti
 * @subpackage fournisseur
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class fournisseurActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->fournisseurs = Doctrine_Core::getTable('fournisseur')
      ->createQuery('a')
      ->execute();
  }
  
  public function executeShow(sfWebRequest $request)
  {
      $this->forward404Unless($fournisseur = Doctrine_Core::getTable('fournisseur')->find(array($request->getParameter('id_fournisseur'))), sprintf('Object fournisseur does not exist (%s).', $request->getParameter('id_fournisseur')));
      $this->fournisseur = $fournisseur;
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new fournisseurForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new fournisseurForm();
    
    $params = $request->getParameter($this->form->getName());
    $params['created_at'] = date('Y-m-d H:i:s', time());
    $params['updated_at'] = date('Y-m-d H:i:s', time());
    $request->setParameter($this->form->getName(), $params);

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($fournisseur = Doctrine_Core::getTable('fournisseur')->find(array($request->getParameter('id_fournisseur'))), sprintf('Object fournisseur does not exist (%s).', $request->getParameter('id_fournisseur')));
    $this->form = new fournisseurForm($fournisseur);
    $this->fournisseur = $fournisseur;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($fournisseur = Doctrine_Core::getTable('fournisseur')->find(array($request->getParameter('id_fournisseur'))), sprintf('Object fournisseur does not exist (%s).', $request->getParameter('id_fournisseur')));
    $this->form = new fournisseurForm($fournisseur);
    $this->fournisseur = $fournisseur;
    
    $params = $request->getParameter($this->form->getName());
    $params['updated_at'] = date('Y-m-d H:i:s', time());
    $request->setParameter($this->form->getName(), $params);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $fournisseur = $form->save();

      $this->redirect('fournisseur/show?id_fournisseur='.$fournisseur->getIdFournisseur());
    }
  }
  
    public function executeList(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $q = Doctrine_Core::getTable('fournisseur')->getFournisseursDatatable($sSearch, $iSortCol_0, $sSortDir_0);
            
            
            $this->pager = new sfDoctrinePager(
                'fournisseur',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url'); 
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {              
                $consulter = '<a class="submit" href="fournisseur/show/id_fournisseur/' . $v->getIdFournisseur() . '">Consulter</a>';
                $aaData[] = array(
                    "0" => $v->getIdFournisseur(),
                    "1" => $v->getNom(),
                    "2" => $v->getNumeroRue() . " rue " . $v->getNomRue() . "<br />" . $v->getCodePostal() . " " . $v->getVille(),
                    "3" => "<a href=mailto:" . $v->getEmail() . ">" . $v->getEmail() . "</a>",
                    "4" => $v->getTelephone(),
                    "5" => $consulter,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    
    public function executeListEchantillons(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $idFournisseur = $request->getParameter('idFournisseur');
            
            $q = Doctrine_Core::getTable('lot')->getLotsFournisseurDatatable($idFournisseur, $sSearch, $iSortCol_0, $sSortDir_0);
            
            
            $this->pager = new sfDoctrinePager(
                'fournisseur',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url');
            $aaData = array();
            $list = $pager->getResults();
           
            
            foreach ($list as $v)
            {
                $consulter = '<a class="submit" href="' . url_for('lot/show?id_lot='. $v->getIdLot()) . '">Consulter</a>';
                
                if($v->getConfidentiel() == 1) $conf = "Oui"; else $conf = "Non";
                $aaData[] = array(
                    "0" => $v->getIdLot(),
                    "1" => $v->getNom(),
                    "2" => $v->getDateRecep(),
                    "3" => $v->getEchantillon()->count(),
                    "4" => $v->getMatiere()->getLibelle(),
                    "5" => $conf,
                    "6" => $consulter,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
}
